package cn.sfrog.project.biz.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.sfrog.common.constant.Constants;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.project.biz.admin.domain.BizServiceUser;
import cn.sfrog.project.biz.admin.domain.vo.*;
import cn.sfrog.project.biz.admin.mapper.BizServiceUserMapper;
import cn.sfrog.project.biz.admin.service.IBizComboService;
import cn.sfrog.project.biz.admin.service.IBizServiceService;
import cn.sfrog.project.biz.admin.service.IBizServiceUserService;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import cn.sfrog.system.domain.vo.app.AppComboQueryVO;
import cn.sfrog.system.domain.vo.app.AppServiceQueryVO;
import cn.sfrog.system.domain.vo.app.AppServiceResultVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 服务套餐和陪诊人员关系Service业务层处理
 */
@Service
public class BizServiceUserServiceImpl extends ServiceImpl<BizServiceUserMapper, BizServiceUser> implements IBizServiceUserService {
    @Resource
    private BizServiceUserMapper bizServiceUserMapper;
    @Autowired
    private IBizServiceService bizServiceService;
    @Autowired
    private IBizComboService bizComboService;

    /**
     * 查询服务套餐和陪诊人员关系
     *
     * @param id 服务套餐和陪诊人员关系主键
     * @return 服务套餐和陪诊人员关系
     */
    @Override
    public BizServiceUserVO selectBizServiceUserById(Long id) {
        return bizServiceUserMapper.selectBizServiceUserById(id);
    }

    /**
     * 查询服务套餐和陪诊人员关系列表
     *
     * @param query 服务套餐和陪诊人员关系
     * @return 服务套餐和陪诊人员关系
     */
    @Override
    public List<BizServiceUserVO> selectBizServiceUserList(BizServiceUserQueryVO query) {
        return bizServiceUserMapper.selectBizServiceUserList(query);
    }

    /**
     * 新增服务套餐和陪诊人员关系
     *
     * @param form 服务套餐和陪诊人员关系
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizServiceUser(BizServiceUserFormVO form) {
        BizServiceUser bizServiceUser = new BizServiceUser();
        BeanUtils.copyProperties(form, bizServiceUser);
        bizServiceUser.setCreateTime(new Date());
        bizServiceUser.setCreateBy(SecurityUtils.getUsername());
        bizServiceUser.setUpdateTime(new Date());
        bizServiceUser.setUpdateBy(SecurityUtils.getUsername());
        return bizServiceUser.insert();
    }

    /**
     * 修改服务套餐和陪诊人员关系
     *
     * @param form 服务套餐和陪诊人员关系
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizServiceUser(BizServiceUserFormVO form) {
        BizServiceUser bizServiceUser = new BizServiceUser();
        BeanUtils.copyProperties(form, bizServiceUser);
        bizServiceUser.setUpdateTime(new Date());
        bizServiceUser.setUpdateBy(SecurityUtils.getUsername());
        return bizServiceUser.updateById();
    }

    /**
     * 批量删除服务套餐和陪诊人员关系
     *
     * @param ids 需要删除的服务套餐和陪诊人员关系主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizServiceUserByIds(Long[] ids) {
            return BizServiceUser.DAO.delete(Wrappers.lambdaQuery(BizServiceUser.class).in(BizServiceUser::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        //return BizServiceUser.DAO.update(Wrappers.lambdaUpdate(BizServiceUser.class).in(BizServiceUser::getId, (Object[]) vo.getIds()).set(BizServiceUser::getStatus, vo.getStatus()));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean relationUser(BizServiceRelationUserFormVO form) {
        LambdaQueryWrapper<BizServiceUser> lambdaQueryWrapper = new LambdaQueryWrapper<BizServiceUser>()
                .eq(BizServiceUser::getType,form.getType())
                .eq(BizServiceUser::getProductId,form.getProductId());
        List<BizServiceUser> bizServiceUsers = BizServiceUser.DAO.selectList(lambdaQueryWrapper);
        List<BizServiceUser> userList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(bizServiceUsers)){
            //先删除 后重新添加
            BizServiceUser.DAO.delete(Wrappers.lambdaQuery(BizServiceUser.class).eq(BizServiceUser::getType,form.getType()).eq(BizServiceUser::getProductId,form.getProductId()));
        }
        Long[] userIds = form.getUserIds();
        for (int i = 0; i < userIds.length; i++) {
            BizServiceUser serviceUser = new BizServiceUser();
            serviceUser.setProductId(form.getProductId());
            serviceUser.setType(form.getType());
            serviceUser.setUserId(userIds[i]);
            userList.add(serviceUser);
        }
        return this.saveBatch(userList,userList.size());
    }

    @Override
    public List<Long> selectRelationUserIdList(BizServiceRelationUserQueryVO queryVO) {
        List<Long> userIds = new ArrayList<>();
        LambdaQueryWrapper<BizServiceUser> lambdaQueryWrapper = new LambdaQueryWrapper<BizServiceUser>()
                .eq(BizServiceUser::getType,queryVO.getType())
                .eq(BizServiceUser::getProductId,queryVO.getProductId());
        List<BizServiceUser> bizServiceUsers = BizServiceUser.DAO.selectList(lambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(bizServiceUsers)){
            userIds = bizServiceUsers.stream().map(BizServiceUser::getUserId).collect(Collectors.toList());
        }
        return userIds;
    }

    @Override
    public List<AppServiceResultVO> selectProductList(BizProductQueryVO queryVO) {
        List<AppServiceResultVO> resultVOList = new ArrayList<>();
        LambdaQueryWrapper<BizServiceUser> lambdaQueryWrapper = new LambdaQueryWrapper<BizServiceUser>()
                .eq(BizServiceUser::getUserId,queryVO.getUserId())
                .eq(BizServiceUser::getStatus,BizServiceUser.STATUS_UP);
        List<BizServiceUser> serviceUserList = BizServiceUser.DAO.selectList(lambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(serviceUserList)){
            if(Constants.TYPE_SERVICE.equals(queryVO.getBuyType())){
                //服务
                List<BizServiceUser> serviceUsers = serviceUserList.stream().filter(x -> x.getType().equals(Constants.TYPE_SERVICE)).collect(Collectors.toList());
                if(CollectionUtil.isNotEmpty(serviceUsers)){
                    List<Long> serviceIds = serviceUsers.stream().map(item -> item.getProductId()).collect(Collectors.toList());
                    AppServiceQueryVO appServiceQueryVO = new AppServiceQueryVO();
                    appServiceQueryVO.setIds(serviceIds);
                    resultVOList = bizServiceService.selectAppServiceList(appServiceQueryVO);
                }
            }else if(Constants.TYPE_COMBO.equals(queryVO.getBuyType())){
                List<BizServiceUser> comboUsers = serviceUserList.stream().filter(x -> x.getType().equals(Constants.TYPE_COMBO)).collect(Collectors.toList());
                if(CollectionUtil.isNotEmpty(comboUsers)){
                    List<Long> comboIds = comboUsers.stream().map(item -> item.getProductId()).collect(Collectors.toList());
                    AppComboQueryVO appComboQueryVO = new AppComboQueryVO();
                    appComboQueryVO.setIds(comboIds);
                    resultVOList = bizComboService.selectAppComboList(appComboQueryVO);
                }
            }
        }
        return resultVOList;
    }
}
